package com.thinkcube.edutainment.adapters;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.thinkcube.edutainment.R;
import com.thinkcube.edutainment.domain.data.CourseCategoryData;
import com.thinkcube.edutainment.domain.data.CourseSubCategoryData;

public class CourseCategoryAdapter extends BaseExpandableListAdapter {

	private LayoutInflater inflater;
	private List<CourseCategoryData> categoryList;

	int selectedGroupIntdex;
	int selectedChildIntdex;

	public CourseCategoryAdapter(Context context,
			List<CourseCategoryData> categoryList) {

		this.categoryList = categoryList;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public CourseSubCategoryData getChild(int groupPosition, int childPosition) {

		return categoryList.get(groupPosition).getSubCategoryData()
				.get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
	
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.expand_child_item, parent,
					false);
		}

		if (selectedChildIntdex != -1 && childPosition == selectedChildIntdex) {
			// view.setBackgroundColor(Color.BLACK);
			convertView.setBackgroundResource(R.drawable.nav_active);
		} else

		{
			convertView.setBackgroundResource(Color.TRANSPARENT);
		}
		TextView tv = (TextView) convertView.findViewById(R.id.ec_child_item);
		tv.setText(getChild(groupPosition, childPosition).getCategoryName());
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {

		return categoryList.get(groupPosition).getSubCategoryData().size();
	}

	@Override
	public String getGroup(int groupPosition) {

		return categoryList.get(groupPosition).getCategoryName();
	}

	@Override
	public int getGroupCount() {

		return categoryList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {

		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean arg1, View convertView,
			ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.expand_item, parent, false);
		}
		TextView tv = (TextView) convertView.findViewById(R.id.expanditem);
		/*
		 * ImageView arrow=(ImageView) convertView.findViewById(R.id.arrow_ic);
		 * if(getChildrenCount(groupPosition)>0) {
		 * arrow.setImageResource(R.drawable.up_arrow); }
		 */

		if (selectedGroupIntdex != -1 && groupPosition == selectedGroupIntdex) {

			convertView.setBackgroundResource(R.drawable.nav_active);
		} else

		{
			convertView.setBackgroundResource(Color.TRANSPARENT);
		}

		tv.setText(getGroup(groupPosition).toString());
		return convertView;
	}

	@Override
	public boolean hasStableIds() {

		return true;  
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {

		return true;  
	}

	public void setSelectedGroupItem(int ind) {
		selectedGroupIntdex = ind;
		notifyDataSetChanged();

	}

	public void setSelectedChildItem(int ind) {
		selectedChildIntdex = ind;
		notifyDataSetChanged();
	}
}
